/*
 * Created by JFormDesigner on Mon Dec 28 16:14:33 CST 2020
 */

package cn.fourhundredandeighteen.view.AdminView.AllcourseFind.UpDate;

import cn.fourhundredandeighteen.UserInfo;
import cn.fourhundredandeighteen.controller.CourseController;
import cn.fourhundredandeighteen.controller.TeacherController;
import cn.fourhundredandeighteen.entity.Course;
import cn.fourhundredandeighteen.entity.Teacher;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.Component.Container;
import cn.fourhundredandeighteen.view.TeacherView.teacherCourseInfo.techercourse;
import cn.fourhundredandeighteen.view.Component.Button;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import javax.swing.*;

/**
 * @author root
 */
public class upData extends JPanel {
    public int num;
    private final ArrayList<JLabel> jLabels = new ArrayList<>();
    private ArrayList<JTextField> jTextFields = new ArrayList<>();
    private cn.fourhundredandeighteen.view.TeacherView.teacherCourseInfo.techercourse techercourse = new techercourse();
    private CourseController courseController = new CourseController();
    private TeacherController teacherController = new TeacherController();
    private Long id;
    public upData(Long id) {
        initComponents();
        this.id=id;
        getdata();
    }
    public void getdata() {
        Map<Integer, String[]> integerMap = techercourse.courseInfo();
        num = integerMap.get(1).length;//计算长度
        jLabels.add(label1);
        jLabels.add(label2);
        jLabels.add(label3);
        jLabels.add(label4);
        jLabels.add(label5);
        jLabels.add(label6);
        jTextFields.add(textField1);
        jTextFields.add(textField2);
        jTextFields.add(textField3);
        jTextFields.add(textField4);
        jTextFields.add(textField5);
        jTextFields.add(textField6);
        for (int i = 0; i < num; i++) {
            jLabels.get(i).setText(integerMap.get(1)[i]);
        }
        Result<Course> courseId = courseController.getCourseId(id);
        jTextFields.get(0).setText(String.valueOf(courseId.getData().getCourseId()));
        jTextFields.get(0).setEditable(false);
        jTextFields.get(1).setText(String.valueOf(courseId.getData().getCourseName()));
        jTextFields.get(2).setText(String.valueOf(courseId.getData().getCourseHour()));
        jTextFields.get(3).setText(String.valueOf(courseId.getData().getCourseScore()));
        jTextFields.get(4).setText(String.valueOf(courseId.getData().getTeacherId()));
        jTextFields.get(5).setText(String.valueOf(courseId.getData().getCourseCount()));
        Container.getInstance().upDatePage();
        this.updateUI();
    }
    public String updatacourseInfo(ArrayList<JTextField> Data) {
        // TODO add your code here
//            if (!NumberUtil.Password(String.valueOf(Data.get(3).getText()))){
//                return "请输入6位数字密码";
//            }
//            if (NumberUtil.Age(String.valueOf(Data.get(2).getText()))){
//                return "请输入正确年龄";
//            }
        //获取需要上传的数据并保存至数组
        try{
            Course course = new Course();
            Date date = new Date();
            course.setCourseId(Long.valueOf(Data.get(0).getText()));
            course.setCourseName(String.valueOf(Data.get(1).getText()));
            course.setCourseHour(Integer.valueOf(Data.get(2).getText()));
            course.setCourseScore(Integer.valueOf(String.valueOf(Data.get(3).getText())));
            course.setTeacherId(Long.valueOf(Data.get(4).getText()));
            Result<Teacher> teacherResult = teacherController.teacherInfo(Long.valueOf(Data.get(4).getText()));
            if (teacherResult.getData() == null){
                return "请检查教师id是否正确";
            }
            course.setCourseCount(Integer.valueOf(Data.get(5).getText()));
            course.setCreateTime(date);
            course.setUpdateTime(date);
            Result result = courseController.upCourse(course);   //课程信息更新接口

            if (result.getMassage()=="SUCCESS"){
                Container.getInstance().upDatePage();
                return "修改成功";

            }else{
                return "修改失败";
            }
        }catch (NumberFormatException numberFormatException){
            return "请输入正确的参数";
        }

    }


    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        String s = updatacourseInfo(jTextFields);
        JOptionPane.showConfirmDialog(null, s, "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        //    label9.setText(infoSet.setStudentInfo(jTextFields));
        //    label9.setForeground(Color.red);
        Container.getInstance().upDatePage();
        Container.getInstance().getRight().updateUI();

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        label4 = new JLabel();
        textField4 = new JTextField();
        label5 = new JLabel();
        textField5 = new JTextField();
        label6 = new JLabel();
        textField6 = new JTextField();
        button1 = new Button();
        label9 = new JLabel();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {87, 69, 0, 0, 207, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {43, 55, 55, 51, 55, 55, 54, 24, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};

        //---- label1 ----
        label1.setText("text");
        label1.setMaximumSize(new Dimension(23, 25));
        add(label1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField1, new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label2 ----
        label2.setText("text");
        add(label2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField2, new GridBagConstraints(2, 2, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label3 ----
        label3.setText("text");
        add(label3, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField3, new GridBagConstraints(2, 3, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label4 ----
        label4.setText("text");
        add(label4, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField4, new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label5 ----
        label5.setText("text");
        add(label5, new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField5, new GridBagConstraints(2, 5, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- label6 ----
        label6.setText("text");
        add(label6, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 15), 0, 0));
        add(textField6, new GridBagConstraints(2, 6, 3, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 25, 0), 0, 0));

        //---- button1 ----
        button1.setText("\u4fee\u6539");
        button1.addActionListener(e -> button1ActionPerformed(e));
        add(button1, new GridBagConstraints(2, 7, 2, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 15), 0, 0));
        add(label9, new GridBagConstraints(4, 7, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JLabel label4;
    private JTextField textField4;
    private JLabel label5;
    private JTextField textField5;
    private JLabel label6;
    private JTextField textField6;
    private JButton button1;
    private JLabel label9;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
